package in.muton.edushare.pages.batch;

import in.muton.edushare.common.UserFilter;
import in.muton.edushare.newentities.BatchUserInviter;
import in.muton.edushare.panels.upload.Uploader;
import in.muton.edushare.panels.upload.exceptions.FileProcessingException;
import in.muton.edushare.panels.upload.exceptions.InvalidUploadFileException;
import in.muton.edushare.wicket.models.CayenneList;
import in.muton.edushare.wicket.models.CayenneModel;
import in.muton.edushare.wicket.models.InnerList;
import in.muton.panels.paginator.EntityListing;
import in.muton.panels.paginator.badgehelpers.user.BatchBadgeRoles;

import java.io.IOException;
import java.util.List;

import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.basic.MultiLineLabel;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.form.upload.FileUpload;
import org.apache.wicket.markup.html.form.upload.FileUploadField;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;

import com.edushare.common.constants.ApplicationConstants;
import com.edushare.common.dao.BatchDAO;
import com.edushare.common.dao.FileDirectoriesDAO;
import com.edushare.common.vo.FileDirectories;
import com.edushare.common.vo.FilesUploaded;
import com.edushare.common.vo.Users;

/** TODO: If agreed, remove search form and user listing */
public class BatchPeople extends InviteUserPage {
	private String searchString = "";
	private String userList = "";
	private CayenneList<Users> cL;
	private String emailID = "";
	private String rollNumber = "";
	private String firstName = "";
	private String lastName = "";
	private String statusString = "";

	public BatchPeople(final PageParameters params) {
		super(params);
		BatchBadgeRoles br = new BatchBadgeRoles(batchModel);
		EntityListing<Users> el = new EntityListing<Users>("thumbList",
				new InnerList<Users>() {
					private static final long serialVersionUID = -56856143743214954L;

					@Override
					public List<IModel<Users>> getObject() {
						return getUsers().getObject();
					}
				}, br, this);
		add(el);
		el.setVisible(false);
		UploadForm<Void> fup = new UploadForm<Void>("uploadForm");
		add(fup);
		addSingleEntryForm();
	}

	private class UploadForm<T> extends Form<T> {

		private static final long serialVersionUID = -5376797921516656349L;
		FileUploadField fufi = null;

		public UploadForm(String id) {
			super(id);
			setMultiPart(true);
			fufi = new FileUploadField("fufi", new Model<FileUpload>());
			add(fufi);
		}

		@Override
		protected void onSubmit() {
			FileUpload fileUpload = fufi.getFileUpload();
			FileDirectories fd = new FileDirectoriesDAO(
					getSessionORMInterface())
					.getOrCreateSystemTempDir(getLoggedInUser());
			Uploader uploader = new Uploader(fileUpload, fd);

			FilesUploaded uploadedFile = null;
			try {
				uploadedFile = uploader.startUploading();
			} catch (InvalidUploadFileException e) {
				warn("Invalid File.");
				e.printStackTrace();
			} catch (IOException e) {
				warn("Error during upload");
				e.printStackTrace();
			} catch (FileProcessingException e) {
				warn("Error in processing this file");
				e.printStackTrace();
			}
			getSessionORMInterface().commitAllChanges();
			setResponsePage(new BatchBulkEntry(new CayenneModel<FilesUploaded>(
					uploadedFile), BatchPeople.this.batchModel));
		}

	}

	@Override
	public String getPageTitle() {
		return batchModel.getObject().getBatchName() + " | Add People | "
				+ ApplicationConstants.APPLICATION_NAME;
	}

	public void setSearchString(String searchString) {
		this.searchString = searchString;
	}

	public String getSearchString() {
		return searchString;
	}

	private CayenneList<Users> getUsers() {
		cL = new CayenneList<Users>(null);
		cL.add(new BatchDAO(getSessionORMInterface()).getUsers(batchModel
				.getObject()));
		cL = UserFilter.filterByString(cL, searchString);
		return cL;
	}

	private void addSingleEntryForm() {
		Form<BatchPeople> f = new Form<BatchPeople>("manualEntry",
				new CompoundPropertyModel<BatchPeople>(this)) {
			@Override
			public void onSubmit() {
				setStatusString(new BatchUserInviter(getSessionORMInterface(),
						batchModel).addDetailedStudent(emailID, rollNumber,
						firstName, lastName));
				setEmailID("");
				setRollNumber("");
				setLastName("");
				setFirstName("");
				setStatusString("");
			}

			private static final long serialVersionUID = 1L;
		};
		add(f);
		f.add(new TextField<String>("emailID"));
		f.add(new TextField<String>("rollNumber"));
		f.add(new TextField<String>("lastName"));
		f.add(new TextField<String>("firstName"));
		f.add(new MultiLineLabel("statusString"));
	}

	public String getEmailID() {
		return emailID;
	}

	public void setEmailID(String emailID) {
		this.emailID = emailID;
	}

	public String getRollNumber() {
		return rollNumber;
	}

	public void setRollNumber(String rollNumber) {
		this.rollNumber = rollNumber;
	}

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	public String getUserList() {
		return userList;
	}

	public void setUserList(String userList) {
		this.userList = userList;
	}

	public void setStatusString(String statusString) {
		this.statusString = statusString;
	}

	public String getStatusString() {
		return statusString;
	}

}
